package com.example.fist.control;

import com.example.fist.dao.DetailsDao;
import com.example.fist.dao.FatherDao;
import com.example.fist.entity.DetailsEntity;
import com.example.fist.entity.FatherEntity;
import com.example.fist.personRepository.DetailsJpaRepository;
import com.example.fist.personRepository.FatherJpaRepository;
import com.example.fist.util.Json.ErrorJson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin(allowCredentials = "true")
public class Match {

    @Autowired
    private DetailsJpaRepository detailsRepository;
    @Autowired
    private FatherJpaRepository fatherRepository;
    @RequestMapping("/match/addmatch")
    public  String addmatch(int com_id, int co_id ,String title ,String mode,String enroll_time,String start_time,
                            String contacts,String phone,String content){
        System.out.println(content);
        DetailsEntity de=new DetailsEntity();
        de.setCoId(co_id);
        de.setComId(com_id);
        de.setTitle(title);
        de.setMode(mode);
        de.setEnrollTime(enroll_time);
        de.setStartTime(start_time);
        de.setReleaseTime(new Date().toString());
        de.setContacts(contacts);
        de.setPhone(Integer.parseInt(phone));
        de.setContent(content);

        de.setModifiTime(new Date());
        de.setIsvisible(1);
        DetailsDao dao=new DetailsDao(detailsRepository);
       int i= dao.addDetails(de);

       if(i>0){
return  new ErrorJson(200,"Ok").toString();
       }
        return new ErrorJson(3001,"添加失败").toString();
    }

    @RequestMapping("/match/modfiymatch")
    public  String modfiymatch(int de_id,int com_id, int co_id ,String title ,String mode,String enroll_time,String start_time,
                            String contacts,String phone,String content){
        DetailsDao dao=new DetailsDao(detailsRepository);


        DetailsEntity de=dao.get(de_id);
        de.setCoId(co_id);
        de.setComId(com_id);
        de.setTitle(title);
        de.setMode(mode);
        de.setEnrollTime(enroll_time);
        de.setStartTime(start_time);
        de.setReleaseTime(new Date().toString());
        de.setContacts(contacts);
        de.setPhone(Integer.parseInt(phone));
        de.setContent(content);

        de.setModifiTime(new Date());
        de.setIsvisible(1);

        int i= dao.addDetails(de);

        if(i>0){
            return  new ErrorJson(200,"Ok").toString();
        }
        return new ErrorJson(3001,"修改失败").toString();
    }

    @RequestMapping("/match/deletematch")
    public  String deletematch(int de_id){
        DetailsDao dao=new DetailsDao(detailsRepository);
        dao.deletDetails(de_id);
      return  new ErrorJson(200,"Ok").toString();
    }

@RequestMapping("/match/get")
    public  String getFathertype(){
    FatherDao dao=new FatherDao(fatherRepository);
    List<FatherEntity> list= dao.Get();

    JSONObject js=new JSONObject();
JSONArray ja=new JSONArray();
    for (FatherEntity e:list
         ) {
        JSONObject jss=new JSONObject();
        try {
            jss.put("id",e.getFaId());
            jss.put("name",e.getFaName());
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        ja.put(jss);
    };

    try {
        js.put("data",ja);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    System.out.println(js.toString());
    return  js.toString();
}

@RequestMapping("/match/findAllPage")
public  String findAllPage(int page){
    DetailsDao dao=new DetailsDao(detailsRepository);
  String json= dao.findAllPage(page);
    return  json;
}

}
